<?php
import("ORG.Util.Function");
import("@.Org.Utils");
import("@.Org.AttributeTool");

class AttributeAction extends CommonAction{	
	
	public function _initialize(){
		parent::_initialize();				
		$param           =    $this->_get('_URL_');
		$controller      =    $param[0];
		$action          =    $param[1];		
		//language
		$lang            =    L($action);  		
		$manage          =    L('manage');
		
		$this->assign("manage",$manage);
		$this->assign("current",$lang);
		$this->assign("lang",$lang);
	}
	
	public function _empty(){
		t('空操作');
	}
	
	// 属性列表
	public function attribute_list(){		
			
		$goods_type	 	= 	isset($_REQUEST['goods_type']) ? intval($_REQUEST['goods_type']) : 0;
	    
	    $list 			= 	get_attrlist();
		
	    $this->assign('attribute_list',    $list);	
	    //$sort_flag  	= 	sort_flag($list['filter']);
	    //$this->assign($sort_flag['tag'], $sort_flag['img']);
	    $this->assign('goods_type_list',  goods_type_list($goods_type)); // 取得商品类型	    
		
	   	/*isAjax*/
	    if($this->isAjax()){
	    	$template	=	"Attribute:l_attribute_list";
	    	layout(false);
	    	$html		=	$this->fetch($template);
	    	$this->ajaxReturn(1,$html,1);
	    }	    
		$this->display();	
	}
	
	/**
	 * 添加属性
	 * 
	 */ 
	public function add_attribute(){
	
	    /* 取得属性信息 */
        $goods_type 	= 	isset($_GET['goods_type']) ? intval($_GET['goods_type']) : 0;
        $attr = array(
            'attr_id' => 0,
            'cat_id' => $goods_type,
            'attr_name' => '',
            'attr_input_type' => 0,
            'attr_index'  => 0,
            'attr_values' => '',
            'attr_type' => 0,
            'is_linked' => 0,
        );

	    $this->assign('attr', $attr);
	    $this->assign('attr_groups', get_attr_groups($attr['cat_id']));
	    	    
		/* 显示模板 */	    
	    /* 取得商品分类列表 */	    
	    $this->assign('goods_type_list', goods_type_list($attr['cat_id']));
		$this->assign('act','add_attribute_ok');
	    $this->display();
	}
	
	// 处理添加属性
	public function add_attribute_ok(){
		// 判断是否存在相同属性
	    if (Utils::attributeExists('attr_name', $_POST['attr_name'], " cat_id = '{$_REQUEST['cat_id']}'")){
	        $this->error("已经存在相同的属性,即将返回上次操作页面",U("attribute/add_attribute"));
	    }	 	
	    // 取得属性信息
	    $attr = array(
	        'cat_id'          => $_POST['cat_id'],
	        'attr_name'       => $_POST['attr_name'],
	        'attr_index'      => $_POST['attr_index'],
	        'attr_input_type' => $_POST['attr_input_type'],
	        'is_linked'       => $_POST['is_linked'],
	        'attr_values'     => isset($_POST['attr_values']) ? $_POST['attr_values'] : '',
	        'attr_type'       => empty($_POST['attr_type']) ? '0' : intval($_POST['attr_type']),
	        'attr_group'      => isset($_POST['attr_group']) ? intval($_POST['attr_group']) : 0
	    );
	    $cat_id				=	$_POST['cat_id'];
	    $result				=	M('Attribute')->add($attr);
	    
	    /*isAjax*/
	    if($this->isAjax()){
	    	if($result){
	    		$this->ajaxReturn(1,U("attribute/add_attribute?goods_type={$cat_id}"),1);
	    	}
	    	else{
	    		$this->ajaxReturn(1,"操作失败",1);
	    	}
	    }
	    else{
		    if($result){
		    	$this->success("操作成功",U("attribute/add_attribute?goods_type={$cat_id}"));
		    }
		    else{
		    	$this->error("操作失败",U("attribute/add_attribute?goods_type={$cat_id}"));
		    }	
	    }	    
	}
	
	// 删除商品属性
	public function del_attribute(){
		$attr_id	=	$_GET['id'];
		// 删除attribute表中
		M('Attribute')->where("attr_id = '{$attr_id}'")->delete();
		// 删除goods_attr表中
		M('GoodsAttr')->where("attr_id = '{$attr_id}'")->delete();
		$this->success("操作成功",U("attribute/attribute_list"));
	}
	
	/**
	 * 修改商品属性
	 * 
	 */
	public function mod_attribute(){		
		$CommonModel	=	new CommonModel();		
	    /*添加还是编辑的标识 */
        $sql 			= 	"SELECT * FROM " . 'ybr_attribute' . " WHERE attr_id = '$_REQUEST[attr_id]'";
        $attr 			= 	$CommonModel->getRow($sql);

	    $this->assign('attr', $attr);
	    $this->assign('attr_groups', get_attr_groups($attr['cat_id']));
	    
	    /* 取得商品分类列表 */
	    $this->assign('goods_type_list', goods_type_list($attr['cat_id']));
		$this->assign("act",'mod_attribute_ok');
	    /* 显示模板 */
	    $this->display('add_attribute');
	}	
	
	/**
	 * 处理修改商品属性
	 *
	 */
	public function mod_attribute_ok(){
		
		/*判断是否存在相同属性*/ 
	    if (Utils::attributeExists('attr_name', $_POST['attr_name'], " cat_id = '{$_REQUEST['cat_id']}'")){
	    	$this->ajaxReturn(0,"已经存在相同的属性,即将返回上次操作页面",1);
	    }	 	
		
	    $cat_id 		= 	$_REQUEST['cat_id'];
	
	    /* 取得属性信息 */
	    $attr = array(
	        'cat_id'          => $_POST['cat_id'],
	        'attr_name'       => $_POST['attr_name'],
	        'attr_index'      => $_POST['attr_index'],
	        'attr_input_type' => $_POST['attr_input_type'],
	        'is_linked'       => $_POST['is_linked'],
	        'attr_values'     => isset($_POST['attr_values']) ? $_POST['attr_values'] : '',
	        'attr_type'       => empty($_POST['attr_type']) ? '0' : intval($_POST['attr_type']),
	        'attr_group'      => isset($_POST['attr_group']) ? intval($_POST['attr_group']) : 0
	    );
		
	    $CommonModel		=	new CommonModel();
	    
        $CommonModel->autoExecute('attribute', $attr, 'UPDATE', "attr_id = '$_POST[attr_id]'");
		
        admin_log($_POST['attr_name'], 'edit', 'attribute');        
        $this->ajaxReturn(1,"操作成功",4);
	}
	
	public function ajax_attribute(){
		$action	=	$_REQUEST['action'];
		switch($action){
			case 'add':
				break;
			case 'batch_del':
				$id_array	=	$_POST['ids'];
				$id_string	=	' (' . implode(',',$id_array) . ') ';
				/*批量删除*/
				M()->execute("DELETE FROM " . 'ybr_attribute'  . " WHERE attr_id in " . $id_string);
			    M()->execute("DELETE FROM " . 'ybr_goods_attr' . " WHERE attr_id in " . $id_string);
				$this->ajaxReturn(1,"操作成功",1);
				break;
			case 'del':				
			    $id = intval($_REQUEST['id']);
			    M()->execute("DELETE FROM " . 'ybr_attribute' . " WHERE attr_id='$id'");
			    M()->execute("DELETE FROM " . 'ybr_goods_attr' . " WHERE attr_id='$id'");				
			    $this->ajaxReturn(1,"操作成功",1);
				break;
			case 'select':
				break;
			case 'edit':
				$field	=	trim($_REQUEST['field']);
				$value	=	trim($_REQUEST['value']);
				$id		=	intval($_REQUEST['id']);
				
			    /*取得该属性所属商品类型id*/
				$cat_id	=	M('Attribute')->where("attr_id = '{$id}'")->getField('cat_id');
			
			    /*检查属性名称是否重复 */
				$is_exsists	=	M("Attribute")->where(" attr_id != '{$id}' and cat_id = '{$cat_id}' and attr_name = '{$value}' ")->find();
			
				if($is_exsists){
					$this->ajaxReturn(0,"存在系统的名称",1);
				}
				else{
					/*不存在,则修改*/
					$result		=	M("Attribute")->where("attr_id = '{$id}'")->setField($field,$value);
					admin_log($val, 'edit', 'attribute');
					/*失败或者成功都默认操作成功*/
					$this->ajaxReturn(1,$value,1);					
					/*if($result){
						$this->ajaxReturn(1,$value,1);	
					}
					else{
						$this->ajaxReturn(0,"",1);
					}*/
				}
				break;
			default:
				break;
		}	
	}
	
	/**
	 * 获取商品类型属性分组
	 * 
	 */
	public function get_attr_groups(){
	
	    $cat_id = intval($_REQUEST['cat_id']);
	    $groups = get_attr_groups($cat_id);
	    	    
	    /*isAjax*/
	    if($this->isAjax()){
	    	$template	=	'Attribute:l_attr_group_list';
	    	$this->assign('attr_groups',$groups);
	    	$html		=	$this->fetch($template);	    	
	    	$this->ajaxReturn(1,$html,1);
	    }
	    return $groups;
	}
		
}





